package com.cainiaoshixi.admin.shiro;

import com.cainiaoshixi.admin.entity.Users;
import com.cainiaoshixi.admin.service.IUserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Author: Chy
 * @Description: shiro自定义Realm
 * @Date: Created at 19:53 2018/2/7
 */
public class LoginRealm extends AuthorizingRealm{
    @Autowired
    private IUserService userService;

    //为当前登录成功的用户授予权限和角色
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    //验证当前登录的用户，获取认证信息。
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String name=(String) token.getPrincipal();//获取用户名
        Users user=userService.getUserByUserName(name);
        if(user!=null){
            AuthenticationInfo authcInfo =new SimpleAuthenticationInfo(user.getName(),user.getPassword(),this.getName());
            return authcInfo;
        }else{
            throw new UnknownAccountException();
        }
    }
}